<template>
    <div id="app">
       <div class="login_container">
           <div class="login_box">
               <div class="avatar_box">
                   <img src="../assets/logo.png" alt="">
               </div>
               <!--登陆表单-->
               <el-form label-width="0px" class="login_form" :model="loginData" :rules="loginRule" ref="loginRef">
                    <el-form-item prop="username">
                        <el-input prefix-icon="el-icon-user" v-model="loginData.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input prefix-icon="el-icon-lock" v-model="loginData.password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item class="login_btns">
                        <el-button type="primary" @click="login">登录</el-button>
                        <el-button type="info" @click="resetForm">重置</el-button>
                    </el-form-item>
               </el-form>
           </div>
       </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单登录数据绑定
      loginData: {
        username: '',
        password: ''
      },
      loginRule: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 25, message: '用户名在5到25位字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 25, message: '密码在5到25位字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginRef.resetFields()
    },
    login () {
      this.$refs.loginRef.validate(async res => {
        if (!res) {
          return false
        }
        const { data: result } = await this.$http.post('login', this.loginData)
        if (result.meta.status !== 200) {
          return this.$message.error('登陆失败！')
        }
        this.$message.success('登陆成功！')
        window.sessionStorage.setItem('token', result.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box{
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        box-shadow: 0 0 10px;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
        }
    }
}
.login_form{
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.login_btns{
    display: flex;
    justify-content: flex-end;
}
</style>
